load INDIPanes

sub IndiBox,#rec,#boxLabel,#altLabel,#expectSex
  ! take one of the two labels
  if #rec!=""
    if @#rec.SEX!=#expectSex
      #boxLabel=#altLabel
    endif
  endif
  #boxTop=#vpos$
  #boxLeft=#hpos$
  ! #boxWidth defined in editing properties
  cell GroupBox,#boxLabel
    set width #boxWidth font "Label Text"
    set border line bordercolor "Pedigree Lines"
    set fontColor "Pedigree Lines"
    !if #rec=@this
    !set cellbackground "Main Individual Background"
    !endif

    #fldWidth=#boxWidth-2*#boxPadding
    #rightSkip=#boxPadding
    #leftSkip=#boxPadding
    newline -9

    if #rec!=""
      cell expression,@#rec.rec
      appendedExpression altview
      set width #fldWidth

      newline
      cell expression,@#rec.BIRT.DATE
      set width #fldWidth AppendedExpression view
      
      newline
      cell expression @#rec.BIRT.PLAC
      set width #fldWidth

    else
      cell static
      set width #fldWidth height 3*#defaultHeight$+2*#lineSkip
    endif

    newline 
    cell static
    set height 2

  EndGroup

  hskip 0
  #moreAncest="No"
  if #hpos$>#col[3]
    if #rec!=""
      if @#rec.FAMC!=""
        cell Palette
        set width #boxSpacing/2 height #boxHeight
        PalDrawColor "Pedigree Lines"
        PalLineWidth 2
        PalMoveTo 0,#boxHeight/2
        PalLine #boxSpacing,0
        #moreAncest="Yes"
      endif
    endif
  endif

  #leftSkip=#leftSkip$
  #rightSkip=#rightSkip$

  newline 4
  #boxHeight=#vpos$-#boxTop
  endif
  if #rec!=""
    if #rec!=@this
      MoveTo #boxLeft,#boxTop+17
      cell LinkButton
      set width #boxWidth height #boxHeight-15 RecordLink #rec border none
      help local("Click to promote this individual to top-left and view ancestral pedigree")
      if #moreAncest="Yes"
        help ". "&local("The line to the right means this individual has documented ancestors.")
      endif
    endif
  endif

endSub

sub FamilyButton,#famsLink,#husbBottom,#col
  if #famsLink!=""
    newline
    #oldVpos=#vpos$
    #famBtn=#husbBottom-#famHeight/2+6.5
    MoveTo #leftSkip+#col*(#boxWidth+#boxSpacing)-#boxSpacing-#famWidth,#famBtn
    cell LinkButton
    set image "family" width #famWidth height #famHeight
    set border shadowlesssquare RecordLink #famsLink PaneID "Group"
    TextExpression @#famsLink.rec
    AutoReload yes
    help local("Click to view family chart for this couple")

    MoveTo #leftSkip,#oldVpos
  endif
endSub

! Main template starts here
if printing is "false"
  gosub INDIPanes,"Pedi"
else
  background "white"
endif

DefaultCellSize 72,16
#lineSkip=0
#cellSkip=0

#minWidth=#leftSkip$+4*(#boxWidth+#boxSpacing)+#rightSkip$
if #minWidth>#rightMargin$-#rightSkip$
  DisplayWidth #minWidth
endif
#famHeight=22
#famWidth=22

#col[1]=#leftSkip+#boxWidth+#boxSpacing
#col[2]=#col[1]+#boxWidth+#boxSpacing
#col[3]=#col[2]+#boxWidth+#boxSpacing

! ----- The Individual and spouse
newline 4
#topChart=#vpos$
gosub IndiBox,@this,local("INDI"),local("INDI"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
if @this.SEX="M"
  gosub IndiBox @spouse,local("Female Spouse"),local("Male Spouse"),"F"
else
  gosub IndiBox @spouse,local("Male Spouse"),local("Female Spouse"),"M"
endif

gosub FamilyButton,@FAMS,#husbLoc,1

! ----- Children
newline #boxRowsep+50
cell label,local("Children")
set width #boxWidth fontColor "Pedigree Lines"

newline 4
cell Palette
set height 3 width #boxWidth-10
PalDrawColor "Pedigree Lines"
PalMoveTo 0,1
PalLine #boxWidth,0

! loop over children in first marriage
newline 1
#i=1
RepeatWith "#child" from @FAMS.CHIL
  newline
  cell LinkButton
  TextExpression @#child.rec
  AppendedExpression altview
  if @#child.FAMS.CHIL!=""
    format #i&". +%@"
  else if @#child.FAMS.i.2.CHIL!=""
    format #i&". +%@"
  else
    format #i&". %@"
  endif
  sizetofit
  set width #boxWidth border none height 20 alignment left
  set font "Hyperlink Text"
  RecordLink #child
  help local("Click to promote this individual to top-left and view ancestral pedigree")
  help ". "&local("A '+' means this child also has children")&"."
  #i+=1
EndRepeat

if #i=1
  newline
  cell static,local("None known")
  set width #boxWidth border none height 20
endif

! ----- parents

newline
MoveTo #col[1]-#boxSpacing,#topChart

cell Palette
set width #boxSpacing height 2*#boxHeight+#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalLineWidth 2
PalMoveTo 0,#boxHeight/2
PalLine #boxSpacing,0
PalMoveTo #boxSpacing/2,#boxHeight/2
PalLine 0,#boxHeight+#boxRowsep/2
PalLine #boxSpacing/2,0

newline
MoveTo #col[1],#topChart
gosub IndiBox,@FAMC.HUSB,local("Father"),local("Mother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[1]
gosub IndiBox,@FAMC.WIFE,local("Mother"),local("Father"),"F"

gosub FamilyButton,@FAMC,#husbLoc,2

! ----- Other Spouses and Children

#leftSkip+=#boxWidth+#boxSpacing
newline #boxRowsep+50
cell label,local("Other Spouses/Children")
set width #boxWidth fontColor "Pedigree Lines"

newline 4
cell Palette
set height 3 width #boxWidth-10
PalDrawColor "Pedigree Lines"
PalMoveTo 0,1
PalLine #boxWidth,0

newline 1
#i=0
RepeatWith "#fams" from @FAMS
  #i+=1
  if #i=1 then
    continue
  endif

  ! spouse button
  newline
  #spouseRec=@spouse.i.#i
  if #spouseRec!=""
    cell LinkButton
    TextExpression @spouse.i.#i.rec
    AppendedExpression altview
    format #i&". %@"
    set alignment left font "Hyperlink Text"
    RecordLink #fams
    help local("Click to promote this individual to top-left and view ancestral pedigree")
  else
    cell static,#i&". "&local("Unknown spouse")
  endif
  set width #boxWidth border none height 20

  #j=1
  RepeatWith "#child" from @FAMS.i.#i.CHIL
    newline
    cell LinkButton,
    TextExpression @#child.rec
    AppendedExpression altview
    if @#child.FAMS.CHIL!=""
      format "    "&#i&"."&#j&" +%@"
    else
      format "    "&#i&"."&#j&" %@"
    endif
    set width #boxWidth border none height 20
    set alignment left font "Hyperlink Text"
    RecordLink #child
    help local("Click to promote this individual to top-left and view ancestral pedigree")
    help ". "&local("A '+' means this child also has children")&"."
    #j+=1
  EndRepeat

  if #j=1
    newline
    cell static,"    "&local("None known")
    set width #boxWidth border none height 20
  endif

EndRepeat
#leftSkip=#leftSkip$

! ----- paternal grandparents
newline
MoveTo #col[2]-#boxSpacing,#topChart

cell Palette
set width #boxSpacing height 2*#boxHeight+#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2
PalLine #boxSpacing,0
PalMoveTo #boxSpacing/2,#boxHeight/2
PalLine 0,#boxHeight+#boxRowsep/2-10
PalLine #boxSpacing/2,0

newline
MoveTo #col[2],#topChart
gosub IndiBox,@FAMC.HUSB.FAMC.HUSB,local("Grandfather"),local("Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[2]
gosub IndiBox,@FAMC.HUSB.FAMC.WIFE,local("Grandmother"),local("Grandfather"),"F"

gosub FamilyButton,@FAMC.HUSB.FAMC,#husbLoc,3

! ----- paternal-paternal great grandparents
newline
MoveTo #col[3]-#boxSpacing,#topChart

cell Palette
set width #boxSpacing height 2*#boxHeight+#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2
PalLine #boxSpacing,0
PalMoveTo #boxSpacing/2,#boxHeight/2
PalLine 0,#boxHeight+#boxRowsep/2-10
PalLine #boxSpacing/2,0

newline
MoveTo #col[3],#topChart
gosub IndiBox,@FAMC.HUSB.FAMC.HUSB.FAMC.HUSB,local("Great Grandfather"),local("Great Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[3]
gosub IndiBox,@FAMC.HUSB.FAMC.HUSB.FAMC.WIFE,local("Great Grandmother"),local("Great Grandfather"),"F"

gosub FamilyButton,@FAMC.HUSB.FAMC.HUSB.FAMC,#husbLoc,4

! ----- paternal-maternal great grandparents
newline #boxRowsep
#husbLoc=#vpos$
MoveTo #col[3]-#boxSpacing,#husbLoc-#boxRowsep-#boxHeight

cell Palette
set width #boxSpacing height 3*#boxHeight+3*#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2+10
PalLine #boxSpacing/2,0
PalLine 0,#boxHeight-10+#boxRowsep
PalLine #boxSpacing/2,0
PalMove -#boxSpacing/2,0
PalLine 0,#boxHeight+#boxRowsep
PalLine #boxSpacing/2,0

newline
MoveTo #col[3],#husbLoc
gosub IndiBox,@FAMC.HUSB.FAMC.WIFE.FAMC.HUSB,local("Great Grandfather"),local("Great Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[3]
gosub IndiBox,@FAMC.HUSB.FAMC.WIFE.FAMC.WIFE,local("Great Grandmother"),local("Great Grandfather"),"F"

gosub FamilyButton,@FAMC.HUSB.FAMC.WIFE.FAMC,#husbLoc,4

! ----- material grandparents
newline #boxRowsep
#midChart=#vpos$
MoveTo #col[2]-#boxSpacing,#midChart-3*#boxHeight-5*#boxRowsep/2

cell Palette
set width #boxSpacing height 5*#boxHeight+3*#boxRowsep
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2+10
PalLine #boxSpacing/2,0
PalLine 0,3*#boxHeight-10+5*#boxRowsep/2
PalLine #boxSpacing/2,0
PalMove -#boxSpacing/2,0
PalLine 0,#boxHeight+#boxRowsep
PalLine #boxSpacing/2,0

newline
MoveTo #col[2],#midChart
gosub IndiBox,@FAMC.WIFE.FAMC.HUSB,local("Grandfather"),local("Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[2]
gosub IndiBox,@FAMC.WIFE.FAMC.WIFE,local("Grandmother"),local("Grandfather"),"F"

gosub FamilyButton,@FAMC.WIFE.FAMC,#husbLoc,3

! ----- maternal-paternal great grandparents
newline
MoveTo #col[3]-#boxSpacing,#midChart

cell Palette
set width #boxSpacing height 2*#boxHeight+#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2
PalLine #boxSpacing,0
PalMoveTo #boxSpacing/2,#boxHeight/2
PalLine 0,#boxHeight+#boxRowsep/2-10
PalLine #boxSpacing/2,0

newline
MoveTo #col[3],#midChart
gosub IndiBox,@FAMC.WIFE.FAMC.HUSB.FAMC.HUSB,local("Great Grandfather"),local("Great Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[3]
gosub IndiBox,@FAMC.WIFE.FAMC.HUSB.FAMC.WIFE,local("Great Grandmother"),local("Great Grandfather"),"F"

gosub FamilyButton,@FAMC.WIFE.FAMC.HUSB.FAMC,#husbLoc,4

! ----- maternal-maternal great grandparents
newline #boxRowsep
#husbLoc=#vpos$
MoveTo #col[3]-#boxSpacing,#husbLoc-#boxRowsep-#boxHeight

cell Palette
set width #boxSpacing height 3*#boxHeight+3*#boxRowsep/2
PalDrawColor "Pedigree Lines"
PalLineWidth 2
PalMoveTo 0,#boxHeight/2+10
PalLine #boxSpacing/2,0
PalLine 0,#boxHeight-10+#boxRowsep
PalLine #boxSpacing/2,0
PalMove -#boxSpacing/2,0
PalLine 0,#boxHeight+#boxRowsep
PalLine #boxSpacing/2,0

newline
MoveTo #col[3],#husbLoc
gosub IndiBox,@FAMC.WIFE.FAMC.WIFE.FAMC.HUSB,local("Great Grandfather"),local("Great Grandmother"),"M"

newline #boxRowsep/2
#husbLoc=#vpos$
HMoveTo #col[3]
gosub IndiBox,@FAMC.WIFE.FAMC.WIFE.FAMC.WIFE,local("Great Grandmother"),local("Great Grandfather"),"F"

gosub FamilyButton,@FAMC.WIFE.FAMC.WIFE.FAMC,#husbLoc,4

hideexcess